<template>
  <div style="padding: 10px">
    <!-- 按钮区域 -->

    <div style="margin: 10px 0">
      <el-button type="primary" @click="ToaddHot">新增</el-button>
    </div>
    <h3>热门电影管理</h3>
    <!-- 搜索框区域 -->

    <div style="margin: 10px 0">
      <el-input
        v-model="searchMovie"
        placeholder="请输入电影名"
        clearable
        style="width: 20%"
      />
      <el-button type="primary" style="margin-left: 10px" @click="findAllHot"
        >查询</el-button
      >
    </div>

    <!-- 表单区域开始 -->
    <el-table :data="HotData" border stripe style="width: 100%">
      <el-table-column prop="id" label="ID" sortable fixed></el-table-column>
      <el-table-column prop="name" label="电影名"></el-table-column>
      <el-table-column prop="actor" label="主演"></el-table-column>
      <el-table-column prop="year" label="上映年份"></el-table-column>

      <el-table-column prop="area" label="地区" sortable width="70">
        <template #default="scope">
          <span>{{
            scope.row.area == 0 ? "内地" : scope.row.area == 1 ? "港台" : "欧美"
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="url" label="地址" width="300"></el-table-column> -->
      <el-table-column
        prop="level"
        sortable
        label="评分"
        width="70"
      ></el-table-column>
      <el-table-column label="封面" width="100">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.img"
            :preview-src-list="[scope.row.img]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <!-- 加上@confirm 只有在确认被点击时才执行 -->
          <el-popconfirm
            title="确认删除吗?"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表单区域结束 -->

    <!-- 分页开始 -->
    <div style="margin: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        v-model:currentPage="pageNum"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 分页结束 -->

    <!-- 添加热门的弹出框 -->
    <el-dialog v-model="dialogVisible" title="添加电影到热门" width="60%">
      <!-- 表单区域开始 -->
      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          sortable
          fixed
          width="60"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="电影名"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="actor"
          label="主演"
          width="100"
        ></el-table-column>

        <el-table-column prop="area" label="地区" width="130" sortable>
          <template #default="scope">
            <span>{{
              scope.row.area == 0
                ? "内地"
                : scope.row.area == 1
                ? "港台"
                : "欧美"
            }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="gmtCreate" label="上传时间"></el-table-column>
        <el-table-column fixed="right" label="操作">
          <template #default="scope">
            <el-button type="success" size="small" @click="handleHot(scope.row)"
              >给它热门</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- 添加热门的弹出框 结束 -->
  </div>
</template>

<script>
import request from "../../utils/request";

export default {
  name: "only",
  components: {},
  data() {
    return {
      searchMovie: "", //定义输入框
      currentPage: 1, //默认是1r
      pageSize: 10, //默认为10
      pageNum: 1,
      total: 0,
      dialogVisible: false, //默认关闭 (添加电影弹出框)
      form: {}, //定义form对象为空
      tableData: [],
      HotData: [], //定义
      sex: 0,
      drawer: false, //默认不显示抽屉
      hotId: 1,
    };
  },
  //页面加载时
  created() {
    this.findAllHot();
  },

  //方法写在data外面
  methods: {
    //查询所有热门电影
    findAllHot() {
      // 查询所有热门电影
      console.log(this.searchMovie);
      request
        .get("/hot/findHotMovie", {
          params: {
            //传入变量
            pageSize: this.pageSize,
            pageNum: this.pageNum,
            name: this.searchMovie,
            hotId: this.hotId,
          },
        })
        .then((res) => {
          console.log(res);
          //重点 将查询到的数据records 返回给表单中的HotData
          (this.HotData = res.data.list), (this.total = res.data.total);
        });
    },

    //查询非热门电影
    findAll() {
      request
        .get("/hot/findHotMovie", {
          params: {
            //传入变量
            pageSize: 9999,
            pageNum: this.pageNum,
            name: this.searchMovie,
            hotId: 0,
          },
        })
        .then((res) => {
          console.log(res);
          //重点 将查询到的数据records 返回给表单中的tableData
          this.tableData = res.data.list;
        });
    },

    //弹出添加热门电影的框
    ToaddHot() {
      this.dialogVisible = true;
      this.findAll();
    },

    //执行添加到热门的方法
    handleHot(row) {
      //将row赋值给form，然后改写hotId为1，
      this.form = JSON.parse(JSON.stringify(row));
      this.form.hotId = 1;
      request.put("/hot/addHot/", this.form).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: "success",
            message: "添加到热门成功",
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
        //执行完了刷新界面
        //this.findAll();

        //关闭弹出框
        this.dialogVisible = false;
        this.findAllHot();
      });
      //重新刷新页面
      this.findAllHot();
    },

    //删除电影
    handleDelete(id) {
      console.log("被删除的热门电影的id为：" + id);
      request.put("/hot/deleteHotMovie/" + id).then((res) => {
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "删除成功",
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
        this.findAllHot(); //重新加载删除之后表格的数据
      });
    },

    //改变条数
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.findAllHot();
    },

    //改变当前页码
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.findAllHot();
    },
  },
};
</script>
